#include "gameroom.h"
#include<QPainter>
#include<QIcon>
#include<QTimer>
#include<QMessageBox>
#include<QPushButton>
#include<QSound>
#include<QTextEdit>
#include<QFile>
gameroom::gameroom(QWidget *parent) : QWidget(parent)
{
    //创建标题，图标
    this->setWindowTitle("游戏房间");
    this->setWindowIcon(QIcon(":res/menu.png"));

    snakeList.push_back(QRectF(this->width()*0.5,this->height()*0.5,snakeWidth,snakeHeight));
    moveUp();
    moveUp();

    //创建食物
    creatFood();
    //渲染小蛇移动
    timer = new QTimer(this);
    connect(timer,&QTimer::timeout,[=](){
        int cnt = 1;

        if(snakeList.front().intersects(food))
        {
            creatFood();
            cnt++;
        }
        while(cnt--)
        {
            switch(MoveDirect)
            {
            case snakeDirect::UP:
                moveUp();
                break;
            case snakeDirect::DOWN:
                moveDown();
                break;
            case snakeDirect::LEFT:
                moveLeft();
                break;
            case snakeDirect::RIGHT:
                moveRight();
                break;
            }
        }

        snakeList.pop_back();
        update();
    });

    //添加开始和暂停按钮
    QPushButton* goButton = new QPushButton(this);
    QPushButton* stopButton = new QPushButton(this);
    QFont font("新宋体",20);
    goButton->setText("开始游戏");
    stopButton->setText("暂停游戏");

    goButton->setFont(font);
    stopButton->setFont(font);

    goButton->move(830,200);
    stopButton->move(830,270);
    //关联开始按钮
    connect(goButton,&QPushButton::clicked,[=](){
        isGameStart = true;
        timer->start(movespead);

        QSound::play(":res/clicked.wav");
    });
    //关联暂停按钮
    connect(stopButton,&QPushButton::clicked,[=](){
        isGameStart = false;
        timer->stop();
        QSound::play(":res/clicked.wav");
    });

    //添加上下左右按钮
    QPushButton* upButton = new QPushButton(this);
    QPushButton* downButton = new QPushButton(this);
    QPushButton* leftButton = new QPushButton(this);
    QPushButton* rightButton = new QPushButton(this);

    upButton->resize(50,50);
    downButton->resize(50,50);
    leftButton->resize(50,50);
    rightButton->resize(50,50);

    upButton->move(875,350);
    downButton->move(875,400);
    leftButton->move(825,400);
    rightButton->move(925,400);

    upButton->setIcon(QIcon(":res/up.png"));
    downButton->setIcon(QIcon(":res/down.png"));
    leftButton->setIcon(QIcon(":res/left.png"));
    rightButton->setIcon(QIcon(":res/right.png"));

    connect(upButton,&QPushButton::clicked,[=](){
        if(MoveDirect != snakeDirect::DOWN)
        {
            MoveDirect = snakeDirect::UP;

        }
        QSound::play(":res/clicked.wav");
    });

    connect(downButton,&QPushButton::clicked,[=](){
        if(MoveDirect != snakeDirect::UP)
        {
            MoveDirect = snakeDirect::DOWN;

        }
        QSound::play(":res/clicked.wav");
    });

    connect(leftButton,&QPushButton::clicked,[=](){
        if(MoveDirect != snakeDirect::RIGHT)
        {
            MoveDirect = snakeDirect::LEFT;

        }
        QSound::play(":res/clicked.wav");
    });


    connect(rightButton,&QPushButton::clicked,[=](){
        if(MoveDirect != snakeDirect::LEFT)
        {
            MoveDirect = snakeDirect::RIGHT;

        }
        QSound::play(":res/clicked.wav");
    });
    //创建退出按钮
    QPushButton* exitButton = new QPushButton(this);
    exitButton->setText("退出游戏");
    exitButton->setFont(font);
    exitButton->move(830,600);

    QMessageBox* mesBox = new QMessageBox(this);
    QPushButton* okButton = new QPushButton(this);
    QPushButton* noButton = new QPushButton(this);

    okButton->setText("OK");
    noButton->setText("NO");
    mesBox->setWindowTitle("确定退出游戏?");
    mesBox->setText("你确定退出游戏吗?");

    mesBox->addButton(okButton,QMessageBox::AcceptRole);
    mesBox->addButton(noButton,QMessageBox::RejectRole);
    //关联退出按钮
    connect(exitButton,&QPushButton::clicked,[=](){
        QSound::play(":res/clicked.wav");
        mesBox->exec();
        if(mesBox->clickedButton() == okButton)
        {
            this->close();
        }
        else
        {
            mesBox->close();
        }
    });


}

void gameroom::paintEvent(QPaintEvent *event)
{
    (void)event;
    QPainter painter(this);

    QPixmap pixmap;

    //绘制地图
    pixmap.load(":res/map.png");
    painter.drawPixmap(0,0,this->width()-200,this->height(),pixmap);

    //绘制操控界面
    pixmap.load(":res/control.png");
    painter.drawPixmap(800,0,this->width()-800,this->height(),pixmap);

    //绘制蛇头
    if(MoveDirect == snakeDirect::UP)
    {
        pixmap.load(":res/snake-up.png");
    }
    else if(MoveDirect == snakeDirect::DOWN)
    {
        pixmap.load(":res/snake-down.png");
    }
    else if(MoveDirect == snakeDirect::LEFT)
    {
        pixmap.load(":res/snake-left.png");
    }
    else
    {
        pixmap.load(":res/snake-right.png");
    }
    auto snakeNode = snakeList.front();
    painter.drawPixmap(snakeNode.x(),snakeNode.y(),snakeNode.width(),snakeNode.height(),pixmap);

    //绘制蛇身
    pixmap.load(":res/body.png");
    for(int i = 1;i<snakeList.size()-1;i++)
    {
        auto node = snakeList.at(i);
        painter.drawPixmap(node.x(),node.y(),node.width(),node.height(),pixmap);
    }

    //绘制蛇尾
    auto snakeTail = snakeList.back();
    painter.drawPixmap(snakeTail.x(),snakeTail.y(),snakeTail.width(),snakeTail.height(),pixmap);

    //绘制食物
    pixmap.load(":res/humber.png");
    painter.drawPixmap(food.x(),food.y(),food.width(),food.height(),pixmap);

    //绘制计分板
    pixmap.load(":res/titel.png");
    painter.drawPixmap(820,50,160,70,pixmap);

    //绘制分数
    QPen pen;
    pen.setColor(Qt::black);
    QFont font("仿宋体",20);
    painter.setFont(font);
    painter.drawText(890,95,QString("%1").arg(snakeList.size()));

    if(chickOver())
    {
        pen.setColor(Qt::red);
        painter.setPen(pen);
        QFont font("仿宋体",50);
        painter.setFont(font);

        painter.drawText(400,400,QString("GAME OVER"));
        timer->stop();
    }

}

void gameroom::moveUp()
{
    //蛇头左上角节点坐标
    QPointF leftTop;
    //蛇头右下角节点坐标
    QPointF rightBottom;
    //取蛇头
    auto snakeNode = snakeList.front();

    int headX = snakeNode.x();
    int headY = snakeNode.y();
    //当穿墙了
    if(headY < 0)
    {
        leftTop = QPointF(headX,this->height()-snakeHeight);
    }
    else
    {
        //没穿墙
        leftTop = QPointF(headX,headY-snakeHeight);
    }
    //更新右下角节点坐标
    rightBottom = leftTop + QPointF(snakeWidth,snakeHeight);
    //更新头节点
    snakeList.push_front(QRectF(leftTop,rightBottom));
}

void gameroom::moveDown()
{
    QPointF leftTop;
    QPointF rightBottom;

    auto snakeNode = snakeList.front();

    int headX = snakeNode.x();
    int headY = snakeNode.y();
    if(headY > this->height())
    {
        leftTop = QPointF(headX,0);
    }
    else
    {
        leftTop = snakeNode.bottomLeft();
    }
    rightBottom = leftTop+QPointF(snakeWidth,snakeHeight);

    snakeList.push_front(QRectF(leftTop,rightBottom));
}

void gameroom::moveLeft()
{
    QPointF leftTop;
    QPointF rightBottom;

    auto snakeNode = snakeList.front();

    int headX = snakeNode.x();
    int headY = snakeNode.y();
    //穿墙了
    if(headX < 0)
    {
        leftTop = QPointF(800-snakeWidth,headY);
    }
    else
    {
        leftTop = QPointF(headX-snakeWidth,headY);
    }
    rightBottom = leftTop + QPointF(snakeWidth,snakeHeight);

    snakeList.push_front(QRectF(leftTop,rightBottom));
}

void gameroom::moveRight()
{
    QPointF leftTop;
    QPointF rightBottom;

    auto snakeNode = snakeList.front();

    int headX = snakeNode.x();
    int headY = snakeNode.y();
    //穿墙
    if(headX > 800-(snakeWidth*2))
    {
        leftTop = QPointF(0,headY);
    }
    else
    {
        leftTop = snakeNode.topRight();
    }
    rightBottom = leftTop+QPointF(snakeWidth,snakeHeight);

    snakeList.push_front(QRectF(leftTop,rightBottom));
}
//判断结束
bool gameroom::chickOver()
{
    for(int i =0;i<snakeList.size();i++)
    {
        for(int j=i+1;j<snakeList.size();j++)
        {
            if(snakeList.at(i) == snakeList.at(j))
            {
                return true;
            }
        }
    }
    return false;
}
//创造食物
void gameroom::creatFood()
{
    food = QRectF(qrand() % (800 / snakeWidth)* snakeWidth,
                  qrand() % (800 / snakeHeight)* snakeHeight,
                  snakeWidth,snakeHeight);
}
